
## The Social Origins of Democracy and Authoritarianism Reconsidered: Prussia and Sweden in Comparison
## by Erik Bengtsson and Felix Kersting
## to be published in Comparative Political Studies, 2025
##
## code for creating Table 1: Sweden – turnout by class, 1911


#setwd("C:/Users/ekh-ebt/Dropbox/__F_Papers/Sonderweg/Sonderweg/Replication/raw")
setwd("~/Library/CloudStorage/Dropbox/09 Sonderweg/Replication/")

library(readxl)
library(dplyr)


participationbyclass<-read_excel("raw/sweden_participation.xlsx", sheet="1911data")

## data cleaning: see to that the numeric variables are recorded as such and that NAs are recorded as such
participationbyclass <- participationbyclass %>%
  mutate(across(c(estateowners, otherworkers), ~as.numeric(na_if(., "NA"))))

## calculate the average turnout for the dataset as a whole
averageturnout <- participationbyclass %>%
  summarise(across(c(estateowners, freeholders, tenants, crofters, farmhands, otherworkers),
                   ~mean(.x, na.rm = TRUE))) %>%
  mutate(estateshare_group = "Mean")

## sort the districts by estate share
participationbyclass <- participationbyclass %>%
  mutate(estateshare_group = if_else(estateshare > median(estateshare, na.rm = TRUE), 
                                     "Above median", "Below median"))

# Calculate turnout by social group depending on whether estate share is above or below the median
turnoutaboveandbelowmedian <- participationbyclass %>%
  group_by(estateshare_group) %>%
  summarise(across(c(estateowners, freeholders, tenants, crofters, farmhands, otherworkers),
                   ~mean(.x, na.rm = TRUE))) 

table1data <- bind_rows(averageturnout, turnoutaboveandbelowmedian)


print(table1data)
library(writexl)
write_xlsx(table1data, "output/tab1.xlsx")
